package com.sxg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxg.convert.SxgGoodsDetailConvert;
import com.sxg.dao.SxgCommentDao;
import com.sxg.dao.SxgGoodsCollectionDao;
import com.sxg.dao.SxgGoodsDetailDao;
import com.sxg.entity.*;
import com.sxg.query.SxgGoodsDetailQuery;
import com.sxg.service.*;
import com.sxg.utils.SecurityUtils;
import com.sxg.utils.code.AjaxResult;
import com.sxg.vo.SxgGoodsDetailVO;
import com.sxg.vo.resp.CollectionRespVo;
import com.sxg.vo.resp.CommentRespVo;
import com.sxg.vo.resp.GoodsDetailRespVo;
import lombok.AllArgsConstructor;
import com.sxg.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品详情信息
 *
 * @author 浪漫写尽歌谱 3298751420@qq.com
 * @since 1.0.0 2024-03-07
 */
@Service
@AllArgsConstructor
public class SxgGoodsDetailServiceImpl extends ServiceImpl<SxgGoodsDetailDao, SxgGoodsDetailEntity> implements SxgGoodsDetailService {

    @Resource
    private SxgImagesService sxgImagesService;

    @Resource
    private SxgCommentDao sxgCommentDao;

    @Resource
    private SxgCommentService sxgCommentService;

    @Resource
    private SxgGoodsCollectionDao sxgGoodsCollectionDao;

    @Resource
    private SxgUserService sxgUserService;


    @Override
    public PageResult<SxgGoodsDetailVO> page(SxgGoodsDetailQuery query) {
        IPage<SxgGoodsDetailEntity> page = baseMapper.selectPage(PageResult.getPage(query), getWrapper(query));

        return new PageResult<>(SxgGoodsDetailConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<SxgGoodsDetailEntity> getWrapper(SxgGoodsDetailQuery query) {
        LambdaQueryWrapper<SxgGoodsDetailEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(SxgGoodsDetailVO vo) {
        SxgGoodsDetailEntity entity = SxgGoodsDetailConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SxgGoodsDetailVO vo) {
        SxgGoodsDetailEntity entity = SxgGoodsDetailConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public SxgGoodsDetailEntity getByGoodsId(Long id) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(SxgGoodsDetailEntity.class)
                .eq(SxgGoodsDetailEntity::getGoodsId, id));
    }

    @Override
    public AjaxResult getDetailByGoodsId(Long id) {
        GoodsDetailRespVo goodsInfo = getBaseMapper().getDetailByGoodsId(id);
        //获取商品图片
        List<SxgImagesEntity> imagesEntities = sxgImagesService.getBaseMapper().selectList(Wrappers.<SxgImagesEntity>lambdaQuery()
                .eq(SxgImagesEntity::getGoodsId, id));
        ArrayList<String> images = new ArrayList<>();
        for (SxgImagesEntity imagesEntity : imagesEntities) {
            if ("1".equals(imagesEntity.getDefaultImg())) {
                goodsInfo.setDetailImage(imagesEntity.getImgUrl());
            } else {
                images.add(imagesEntity.getImgUrl());
            }
        }
        //获取商品评论
        List<CommentRespVo> commentRespVos = sxgCommentDao.selectByGoodsIdCount2(id);
        List<CommentRespVo> collect = commentRespVos.stream().peek(commentRespVo -> {
            Long userId = commentRespVo.getUserId();
            SxgUserEntity sxgUser = sxgUserService.selectUserByUserId(userId);
            commentRespVo.setUserAvatar(sxgUser.getAvatar());
            commentRespVo.setUserNickname(sxgUser.getNickName());
        }).collect(Collectors.toList());
        Long commentCount = sxgCommentService.getBaseMapper().selectCount(Wrappers.<SxgCommentEntity>lambdaQuery().eq(SxgCommentEntity::getGoodsId, id).eq(SxgCommentEntity::getIsDelete, "0")
                .eq(SxgCommentEntity::getTopCommentId, 0));
        goodsInfo.setComments(collect);
        goodsInfo.setImages(images);
        goodsInfo.setCommentsTotal(commentCount);
        //获取当前商品用户是否有收藏
        Long userId = SecurityUtils.getUserId();
        CollectionRespVo byUserId = sxgGoodsCollectionDao.getByUserId(userId, id);
        if (byUserId == null) {
            goodsInfo.setIsCollect(false);
        } else {
            goodsInfo.setIsCollect(true);
        }
        return AjaxResult.success(goodsInfo);
    }

}